﻿using RAP.Framework.Libary.DomTemplateBuilder.Element.Conditional;
using RAP.Framework.Libary.Utils;
using System;
using System.Collections.Generic;
using System.Text;
using RAP.Framework.Libary.DomTemplateBuilder.Interface;

namespace RAP.Framework.Libary.DomTemplateBuilder.Handler.Conditional
{
	public class IfHandler : DynamicHandler, IConditionHandler
    {
        public virtual new If Element
        {
            get
            {
                return base.Element as If;
            }
        }

        /// <summary>
        /// 获取Else节点的Handler
        /// </summary>
        public IHandler ElseHandler
        {
            get
            {
                if ( this.Element.Else == null )
                {
                    return null;
                }
                return this.Element.Else.Handler;
            }
        }

        private IList<IConditionHandler> _elseIfHandlers = new List<IConditionHandler>();
        /// <summary>
        /// 获取ElseIf节点的Handler
        /// </summary>
        public IList<IConditionHandler> ElseIfHandlers
        {
            get
            {
                if ( this.Element.ElseIfs.IsNullOrEmpty() )
                {
                    return _elseIfHandlers;
                }
                if ( _elseIfHandlers.Count > 0 )
                {
                    return _elseIfHandlers;
                }
                this.Element.ElseIfs.Map<IElement>( ele =>
                {
                    _elseIfHandlers.Add( ele.Handler as IConditionHandler );
                    return true;
                } );
                return _elseIfHandlers;
            }
        }

        public virtual bool IsCondition ( object rootParamObject, object parentParamObject, ITemplateRenderContext context )
        {
            var handler = this.Element.GetExpressionHandler( rootParamObject, parentParamObject, context );
            if ( handler == null )
            {
                throw new NullReferenceException( "If节点的表达式语法错误，获取表达式Handler失败！" );
            }
            return handler( rootParamObject, parentParamObject, context );
        }

        public override void DoRender ( StringBuilder writer, object rootParamObject, object parentParamObject, ITemplateRenderContext context )
        {
            if ( this.IsCondition( rootParamObject, parentParamObject, context ) )
            {
                base.DoRender( writer, rootParamObject, parentParamObject, context );
            }
            else
            {
                var paramObject = this.GetParamObject( rootParamObject, parentParamObject, context );
                var elseIfHandlers = this.ElseIfHandlers;
                if ( elseIfHandlers.IsNotNullOrEmpty() )
                {
                    //执行所有ElseIf节点
                    foreach ( var elseIfHandler in elseIfHandlers )
                    {
                        if ( elseIfHandler.IsCondition( rootParamObject, paramObject, context ) )
                        {
                            ( elseIfHandler as IHandler ).DoRender( writer, rootParamObject, paramObject, context );
                            return;
                        }
                    }
                }
                //执行Else节点
                var elseHandler = this.ElseHandler;
                if ( elseHandler != null )
                {
                    elseHandler.DoRender( writer, rootParamObject, paramObject, context );
                }
            }
        }
    }
}
